﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :Experience.aspx.cs
//        description :经验分享列表页面
//
//        created by 邹兴武 at  2010-12-30
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Share
{
    public partial class Experience : System.Web.UI.Page
    {
        private readonly D_T_Experience_Exp _dal = new D_T_Experience_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.MainInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              SetBut,
             UserConst.Exp_ToolBarErr,
              SetText()
           );

            if (!IsPostBack)
            {
                SetData(0);
            }
        }
        private huaguwang.WebControls.WebUserToolBar.SelectItem[] SetText()
        {
            huaguwang.WebControls.WebUserToolBar.SelectItem[] list = { 
                 new huaguwang.WebControls.WebUserToolBar.SelectItem()
              {
                  FuncGetText = () => txtTitle.Text.Trim(),
                  FuncSetText = v => { txtTitle.Text = v; },
                  Name = txtTitle.ID
              },
               new huaguwang.WebControls.WebUserToolBar.SelectItem()
               {
                   FuncGetText = () => txtCateTitle.Text.Trim(),
                   FuncSetText = v => { txtCateTitle.Text = v; },
                   Name = txtCateTitle.ID
               }                                                                     
            };
            return list;
        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 0:
                case 1:
                case 2:
                    but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                    break;
                case 4:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                    but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        private void SetData(int begin)
        {
            AdminPublic.Try(() =>
            {
                var count = 0;
                var title = this.WebUserToolBar1.MainGetQuery(txtTitle.ID);
                var catetitle = this.WebUserToolBar1.MainGetQuery(txtCateTitle.ID);
                this.Repeater1.DataSource = _dal.GetList(begin, Pager.PageSize, title, catetitle, out count);
                this.Repeater1.DataBind();
                this.Pager.RecordCount = count;
            });
        }

        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            SetData((e.NewPageIndex - 1) * Pager.PageSize);
        }

        /// <summary>
        /// 解冻按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以操作的信息");
                return;
            }

            var refInt = _dal.UpdateState(intA,true);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.other, "解除封冻的分享信息", intA);
                this.Exp_PageAlter("解冻操作成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("解冻操作失败");
                return;
            }
        }

        /// <summary>
        /// 封冻按钮的事件
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以操作的信息");
                return;
            }

            var refInt = _dal.UpdateState(intA, false);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.other, "封冻分享信息", intA);
                this.Exp_PageAlter("封冻操作成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("封冻操作失败");
                return;
            }
        }

        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以删除的信息");
                return;
            }

            var refInt = _dal.UpdateFlag(intA);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.delete, "分享信息", intA);
                this.Exp_PageAlter("删除成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("删除操作失败");
                return;
            }
        }

        /// <summary>
        /// 查询按钮的事件
        /// </summary>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            WebUserToolBar1.MainSetQuery();
            SetData(0);
        }
               
        /// <summary>
        /// 详细按钮的事件 
        /// </summary>
        protected void WebUserToolBar1_Button4(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("experience_e.aspx?id=" + strID + "&" + sKey, false);
        }

        /// <summary>
        /// 推荐按钮的事件 
        /// </summary>
        protected void WebUserToolBar1_Button5(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以操作的信息");
                return;
            }

            var refInt = _dal.UpdateRem(intA, true);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.other, "推荐分享信息", intA);
                this.Exp_PageAlter("推荐操作成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("推荐操作失败");
                return;
            }
        }

        /// <summary>
        /// 置顶按钮的事件 
        /// </summary>
        protected void WebUserToolBar1_Button6(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以操作的信息");
                return;
            }

            var refInt = _dal.UpdateTopis(intA, 1);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.other, "置顶分享信息", intA);
                this.Exp_PageAlter("置顶操作成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("置顶操作失败");
                return;
            }
        }

        /// <summary>
        /// 取消推荐按钮的事件 
        /// </summary>
        protected void WebUserToolBar1_Button7(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以操作的信息");
                return;
            }

            var refInt = _dal.UpdateRem(intA, false);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.other, "取消推荐分享信息", intA);
                this.Exp_PageAlter("取消推荐操作成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("取消推荐操作失败");
                return;
            }
        }

        /// <summary>
        /// 取消置顶按钮的事件 
        /// </summary>
        protected void WebUserToolBar1_Button8(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以操作的信息");
                return;
            }

            var refInt = _dal.UpdateTopis(intA, 0);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.other, "取消置顶分享信息", intA);
                this.Exp_PageAlter("取消置顶操作成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("取消置顶操作失败");
                return;
            }
        }

    }
}
